package com.camp.android.trip.n.track.menu;

import java.text.SimpleDateFormat;
import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RatingBar;
import android.widget.TextView;

import com.camp.android.trip.n.track.DatabaseManager;
import com.camp.android.trip.n.track.Item;
import com.camp.android.trip.n.track.R;
import com.camp.android.trip.n.track.Trip;
import com.camp.android.trip.n.track.trip.TripItineraryActivity;

public class UpcomingListAdapter extends BaseAdapter implements OnClickListener {

	private ArrayList<Trip> itemList = null;
	private LayoutInflater inflater = null;
	private DatabaseManager dbmanager = null;
	private Activity activity = null;
	
	public UpcomingListAdapter(LayoutInflater inflater,Activity activity) {
		
		// Add variables
		this.inflater = inflater;
		this.activity = activity;
		this.dbmanager = new DatabaseManager(activity);
		
		// Fill itemList
		itemList = new ArrayList<Trip>();
		itemList = dbmanager.getFutureTrips();
	}
	
	@Override
	public int getCount() {
		return itemList.size();
	}
 
	@Override
	public Object getItem(int position) {
		Log.i("Tag","Getting item");
		return itemList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return itemList.get(position).id;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		// Get custom item view
		View itemView = inflater.inflate(R.layout.menu_item, parent, false);
		// Get item object
		Trip trip = (Trip)getItem(position);

		float trip_rating = 0;
		
		ArrayList<Item> items = new DatabaseManager(activity).getItems(trip.id);
		if(items.size() > 0) {
			
			for(int i = 0; i < items.size(); i++) {
				trip_rating += items.get(i).rating;
			}
			trip_rating = trip_rating / items.size();
		}
		Log.i("RATING2",trip.items.size()+" --- "+trip_rating);
		// Fill custom view with object's properties
		((TextView) itemView.findViewById(R.id.item_name)).setText(trip.name);
		((RatingBar) itemView.findViewById(R.id.item_rating)).setRating(trip_rating);
		((TextView) itemView.findViewById(R.id.item_id)).setText(trip.id+"");
		// Initialize date format
		SimpleDateFormat dateFormat = new SimpleDateFormat("MMM dd");
		((TextView) itemView.findViewById(R.id.item_date)).setText(dateFormat.format(trip.start.getTime()));
		
		itemView.setOnClickListener(this);
		return itemView;

	} 

	@Override
	public void onClick(View v) {
		Log.i("TAG","Item Clicked "+ ((TextView)v.findViewById(R.id.item_id)).getText());
		 
		// Create intent to go to TripItineraryActivity
		// And add item's id to extra for initialization
		Intent intent = new Intent(activity,TripItineraryActivity.class);
		intent.putExtra("trip_id", ((TextView)v.findViewById(R.id.item_id)).getText());
		activity.startActivity(intent);
	}
}
